<?php
    if ( ! function_exists('get_banner')){
	function get_banner($location)
	{
	    $CI =& get_instance();
	    $CI->load->model('admin/banner_model');
	    $time = time();
	    $var = $CI->banner_model->get_banner($location,$time);
	    $site_url = site_url();
	    if(isset($var[0]->used)){
		
		shuffle($var);
		
		if($var[0]->used==1){
		    echo "<a href='{$var[0]->url}' rel='nofollow' target='_blank'><img src='{$site_url}assets/image/banner/{$var[0]->id}/{$var[0]->img}' class='img_link' /></a>";
		}else{
		    echo $var[0]->code;
		}
	    }else{
		echo "<a href='{$site_url}' ><img src='{$site_url}assets/image/banner/default/{$location}.jpg' class='img_link' /></a>";
	    }
	}
    }
    
    if ( ! function_exists('get_user_box')){
	function get_user_box()
	{
	    $CI =& get_instance();
	    $CI->load->model('user_model','user_model');
	    
	    $site_url = site_url();
	    if($CI->session->userdata('logged_in') == TRUE){
		$time = time();
		
		$user_info = $CI->user_model->getUserInfo($CI->session->userdata('username'));
		
		if(isset($user_info['gold_active']) AND $user_info['gold_active']==1 AND $user_info['gold_start']<=$time AND $user_info['gold_finish']>=$time){
		    $user_level = $CI->lang->line('gold_level');
		}else{
		    $user_level = $CI->lang->line('general_level');
		}
		
		return <<<ddd
		    <div id="user_info_block">
			<p class="name">{$user_info['firstname']} {$user_info['lastname']}</p>
			<p class="level">{$user_level}</p>
			<p class="link"><a href="{$site_url}user/dashboard" title="Dashboard">Dashboard</a><a href="{$site_url}user/logout" title="Log out">Log out</a></p>
		    </div>
ddd;
	    }else{
		return <<<ddd
		<div id="login_block">
		    <form action="{$site_url}user/login_auth" method="post" id="login_block_frm">
			<input name="username" type="text" id="login_name" />
			<input name="password" type="password" id="login_pass" />
			<img id="login_bt" src="{$site_url}assets/image/button/login_bt.jpg" onclick="check_login_block_frm()" class="img_link" />
		    </form>
		    <ul>
			<li><a href="{$site_url}user/register" title="สมัครสมาชิก">สมัครสมาชิก</a></li>
			<li><a href="{$site_url}user/forgotpass" title="ลืมรหัสผ่าน">ลืมรหัสผ่าน</a></li>
			<div class="clear"></div>
		    </ul>
		</div>
ddd;
	    }
	}
    }
    
    if ( ! function_exists('get_cat_option')){
	function get_cat_option($cat_id=0)
	{
	    $CI =& get_instance();
	    $CI->load->model('admin/category_model','category_model');
	    $option = $CI->category_model->lists(0,1,500);
	    foreach ($option as $v) {
		$selected = '';
		if($v->id==$cat_id){
		    $selected = 'selected="selected"';
		}
		echo "<option value='{$v->id}' {$selected}>{$v->name_th}</option>";
	    }
	}
    }
    
    if ( ! function_exists('get_sub_cat_option')){
	function get_sub_cat_option($parent_id=0,$cat_id=0)
	{
	    if($parent_id!=0){
		$CI =& get_instance();
		$CI->load->model('admin/category_model','category_model');
		$option = $CI->category_model->lists($parent_id,1,500);
		$selected = '';
		if($cat_id==0){
		    $selected = 'selected="selected"';
		}
		echo '<option value="0" '.$selected.'>หมวดหมู่ย่อย</option>';
		
		foreach ($option as $v) {
		    $selected = '';
		    if($v->id==$cat_id){
			$selected = 'selected="selected"';
		    }
		    echo "<option value='{$v->id}' {$selected}>{$v->name_th}</option>";
		}
	    }else{
		echo '<option value="0" selected="selected">หมวดหมู่ย่อย</option>';
	    }
	}
    }
    
    if ( ! function_exists('get_geography_option')){
	function get_geography_option($geo_id=0)
	{
	    $CI =& get_instance();
	    $CI->load->model('admin/province_model','province_model');
	    $option = $CI->province_model->geography_lists(1,500);
	    foreach ($option as $v) {
		$selected = '';
		if($v->GEO_ID==$geo_id){
		    $selected = 'selected="selected"';
		}
		echo "<option value='{$v->GEO_ID}' {$selected}>{$v->GEO_NAME}</option>";
	    }
	}
    }
    
    if ( ! function_exists('get_geography_checkbox')){
	function get_geography_checkbox($geo_id=array())
	{
	    $CI =& get_instance();
	    $CI->load->model('admin/province_model','province_model');
	    $option = $CI->province_model->geography_lists(1,500);
	    foreach ($option as $v) {
		$selected = '';
		if(is_array($geo_id) AND in_array($v->GEO_ID,$geo_id)){
		    $selected = 'checked="checked"';
		}
		echo '<label><input type="checkbox" name="geography[]" value="'.$v->GEO_ID.'" class="s_geography" '.$selected.' /> '.$v->GEO_NAME.'</label><br/>';
	    }
	}
    }
    
    if ( ! function_exists('get_province_option')){
	function get_province_option($geo_id=0,$province_id=0)
	{
	    $CI =& get_instance();
	    $CI->load->model('admin/province_model','province_model');
	    
	    $option = $CI->province_model->lists($geo_id,1,500);
	    
	    $selected = '';
	    if($province_id==0){
		$selected = 'selected="selected"';
	    }
	    echo "<option value='0' {$selected}>จังหวัด</option>";
	    
	    foreach ($option as $v) {
		$selected = '';
		if($v->PROVINCE_ID==$province_id){
		    $selected = 'selected="selected"';
		}
		echo "<option value='{$v->PROVINCE_ID}' {$selected}>{$v->PROVINCE_NAME}</option>";
	    }
	}
    }
    
    if ( ! function_exists('get_company_nav')){
	function get_company_nav($all,$page_limit,$current,$url)
	{
	    $num_page = ceil($all/$page_limit);
	    $nav = '';
	    //echo $num_page;
	    if($num_page>1){
		if($current>1){
		    $nav .= '<a href="'.str_replace('%page%',1,$url).'"><<</a> <a href="'.str_replace('%page%',($current-1),$url).'"><</a>';
		}
		
		$i = 1;
		for($i;$i<=$num_page;$i++){
		    if($i==$current){
			$nav .= " $i"; 
		    }else{
			$nav .= ' <a href="'.str_replace('%page%',$i,$url).'">'.$i.'</a>';
		    }
		}
		
		if($current<$num_page){
		    $nav .= ' <a href="'.str_replace('%page%',($current+1),$url).'">></a> <a href="'.str_replace('%page%',$num_page,$url).'">>></a>';
		}
	    }
	    return $nav;
	}
    }
    
    if ( ! function_exists('breadcrumb')){
	function breadcrumb()
	{
	    $CI =& get_instance();
	    $CI->lang->load('main', 'thai');
	    $segs = $CI->uri->segment_array();
	    //var_dump($segs);
	    
	    $breadcrumb = '<a href="'.site_url().'" title="'.$CI->lang->line('home').'">'.$CI->lang->line('home').'</a>';
	    switch($segs[1]){
		case 'about':
		    $breadcrumb .= ' > <a href="'.site_url().'about" title="'.$CI->lang->line('about').'">'.$CI->lang->line('about').'</a>';
		    break;
		case 'sitemap':
		    $breadcrumb .= ' > <a href="'.site_url().'sitemap" title="'.$CI->lang->line('sitemap').'">'.$CI->lang->line('sitemap').'</a>';
		    break;
		case 'contact':
		    $breadcrumb .= ' > <a href="'.site_url().'contact" title="'.$CI->lang->line('contact').'">'.$CI->lang->line('contact').'</a>';
		    break;
		case 'directory':
		    $breadcrumb .= ' > <a href="'.site_url().'directory" title="'.$CI->lang->line('directory').'">'.$CI->lang->line('directory').'</a>';
		    break;
		case 'agent':
		    $breadcrumb .= ' > <a href="'.site_url().'agent" title="'.$CI->lang->line('agent').'">'.$CI->lang->line('agent').'</a>';
		    break;
		case 'subscribe':
		    $breadcrumb .= ' > <a href="'.site_url().'subscribe" title="'.$CI->lang->line('subscribe').'">'.$CI->lang->line('subscribe').'</a>';
		    break;
		case 'technology':
		    $breadcrumb .= ' > <a href="'.site_url().'technology" title="'.$CI->lang->line('technology').'">'.$CI->lang->line('technology').'</a>';
		    break;
		case 'banner':
		    $breadcrumb .= ' > <a href="'.site_url().'banner" title="'.$CI->lang->line('banner').'">'.$CI->lang->line('banner').'</a>';
		    break;
		case 'search':
		    $breadcrumb .= ' > <a href="'.site_url().'search?'.$_SERVER['QUERY_STRING'].'" title="'.$CI->lang->line('search result').'">'.$CI->lang->line('search result').'</a>';
		    break;
		case 'category':
		    $breadcrumb .= ' > <a href="'.site_url().'directory" title="'.$CI->lang->line('directory').'">'.$CI->lang->line('directory').'</a>';
		    
		    if(isset($segs[2])){
			$str = explode('-',$segs[2]);
			$breadcrumb .= ' > <a href="'.site_url().'category/'.$segs[2].'" title="'.urldecode($str[1]).'">'.urldecode($str[1]).'</a>';
		    }
		    
		    if(isset($segs[3])){
			$str = explode('-',$segs[3]);
			$breadcrumb .= ' > <a href="'.site_url().'category/'.$segs[2].'/'.$segs[3].'" title="'.urldecode($str[1]).'">'.urldecode($str[1]).'</a>';
		    }
		    break;
		case 'gold':
		    $breadcrumb .= ' > <a href="'.site_url().'gold" title="'.$CI->lang->line('gold register').'">'.$CI->lang->line('gold register').'</a>';
		    break;
		case 'user':
		    $breadcrumb .= ' > <a href="'.site_url().'user" title="'.$CI->lang->line('user page').'">'.$CI->lang->line('user page').'</a>';
		    switch($segs[2]){
			case 'dashboard':
			    $breadcrumb .= ' > <a href="'.site_url().'user/dashboard" title="'.$CI->lang->line('dashboard').'">'.$CI->lang->line('dashboard').'</a>';
			    break;
			case 'forgotpass':
			    $breadcrumb .= ' > <a href="'.site_url().'user/forgotpass" title="'.$CI->lang->line('forget_password').'">'.$CI->lang->line('forget_password').'</a>';
			    break;
			case 'login':
			    $breadcrumb .= ' > <a href="'.site_url().'user/login" title="'.$CI->lang->line('login').'">'.$CI->lang->line('login').'</a>';
			    break;
			case 'email-confirm':
			    $breadcrumb .= ' > <a href="'.site_url().'user/email-confirm" title="'.$CI->lang->line('email_confirm').'">'.$CI->lang->line('email_confirm').'</a>';
			    break;
			case 'register':
			    $breadcrumb .= ' > <a href="'.site_url().'user/register" title="'.$CI->lang->line('register').'">'.$CI->lang->line('register').'</a>';
			    break;
			case 'build_website':
			    $breadcrumb .= ' > <a href="'.site_url().'user/website" title="'.$CI->lang->line('build_website').'">'.$CI->lang->line('build_website').'</a>';
			    break;
		    }
		    break;
	    }
	    echo $breadcrumb;
	}
    }
    
    if ( ! function_exists('get_new_tech')){
	function get_new_tech()
	{
	    $CI =& get_instance();
	    
	    $CI->load->model('admin/technology_model','technology_model');
	    $content = $CI->technology_model->lists(1,1);
	    return substr_utf8($content['0']->detail , 0 , 225);
	}
    }
    
    if ( ! function_exists('substr_utf8')){
	function substr_utf8( $str, $start_p , $len_p)
	{
	    return preg_replace( '#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$start_p.'}'.'((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$len_p.'}).*#s','$1' , $str );
	}
    }
    
    
/* End of file project_helper.php */
/* Location: ./application/helpers/project_helper.php */